package online.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @Auther: 狼芒
 * @Date: 2021/11/7 16:12
 * @Descrintion: RabbitMq学习
 * @version: 1.0
 */
@Configuration
public class TtlQueueConfig {

    //普通交换机名称
    public static final String COMMON_SWITCH = "common_switch";
    //死信交换机名称
    public static final String DEAD_LETTER_SWITCH = "dead_letter_switch";
    //备份交换机名称
    public static final String BACKUP_SWITCH = "backup_switch";
    //备份交换机名称
    public static final String BACKUP_QUEUE = "backup_queue";
    //备份交换机名称
    public static final String WARNING_QUEUE = "warning_queue";
    //普通对列名称
    public static final String COMMON_FASTER_QUEUE = "common_faster_queue";
    public static final String COMMON_SLOWER_QUEUE = "common_slower_queue";
    public static final String COMMON_ALL_TIME_QUEUE = "common_all_time_queue";
    //死信队列名称
    public static final String DEAD_LETTER_QUEUE = "dead_letter_queue";
    //交换机路径
    public static final String COMMON_SWITCH_TO_COMMON_FASTER_QUEUE_PATH = "common_switch_to_common_faster_queue_path";
    public static final String COMMON_SWITCH_TO_COMMON_SLOWER_QUEUE_PATH = "common_switch_to_common_slower_queue_path";
    public static final String COMMON_SWITCH_TO_COMMON_ALL_TIME_QUEUE_PATH = "common_switch_to_common_all_time_queue_path";
    public static final String COMMON_ALL_QUEUE_TO_DEAD_LETTER_SWITCH_PATH = "common_all_queue_to_dead_letter_switch_path";

    /**
     * 声明普通交换机
     *
     * @return
     */
    @Bean("CommonSwitch")
    public DirectExchange CommonSwitch() {
//        return new DirectExchange(COMMON_SWITCH);
        return ExchangeBuilder.directExchange(COMMON_SWITCH).durable(true).withArgument("alternate-exchange", BACKUP_SWITCH).build();
    }

    /**
     * 声明死信队列交换机
     *
     * @return
     */
    @Bean("DeadLetterSwitch")
    public DirectExchange DeadLetterSwitch() {
        return new DirectExchange(DEAD_LETTER_SWITCH);
    }

    /**
     * 声明备份队列交换机
     *BACKUP_SWITCH
     * @return
     */
    @Bean("BackupSwitch")
    public FanoutExchange BackupSwitch() {
        return new FanoutExchange(BACKUP_SWITCH);
    }

    /**
     * 声明较快的队列
     */
    @Bean("CommonFasterQueue")
    public Queue CommonFasterQueue() {
        HashMap<String, Object> arguments = new HashMap<>(3);
        //设置死信交换机
        arguments.put("x-dead-letter-exchange", DEAD_LETTER_SWITCH);
        //设置死信路由Key
        arguments.put("x-dead-letter-routing-key", COMMON_ALL_QUEUE_TO_DEAD_LETTER_SWITCH_PATH);
        //设置TTL  单位时间是ms
        arguments.put("x-message-ttl", 10000);
        return QueueBuilder.durable(COMMON_FASTER_QUEUE)
                .withArguments(arguments)
                .build();
    }

    /**
     * 声明较慢的队列
     */
    @Bean("CommonSlowerQueue")
    public Queue CommonSlowerQueue() {
        HashMap<String, Object> arguments = new HashMap<>(3);
        //设置死信交换机
        arguments.put("x-dead-letter-exchange", DEAD_LETTER_SWITCH);
        //设置死信路由Key
        arguments.put("x-dead-letter-routing-key", COMMON_ALL_QUEUE_TO_DEAD_LETTER_SWITCH_PATH);
        //设置TTL  单位时间是ms
        arguments.put("x-message-ttl", 40000);
        return QueueBuilder.durable(COMMON_SLOWER_QUEUE)
                .withArguments(arguments)
                .build();
    }


    /**
     * 声明一个任意时间过期的队列
     */
    @Bean("CommonAllTimeQueue")
    public Queue CommonAllTimeQueue() {
        HashMap<String, Object> arguments = new HashMap<>(2);
        //设置死信交换机
        arguments.put("x-dead-letter-exchange", DEAD_LETTER_SWITCH);
        //设置死信路由Key
        arguments.put("x-dead-letter-routing-key", COMMON_ALL_QUEUE_TO_DEAD_LETTER_SWITCH_PATH);
        //设置TTL  单位时间是ms
//        arguments.put("x-message-ttl", 40000);
        return QueueBuilder.durable(COMMON_ALL_TIME_QUEUE)
                .withArguments(arguments)
                .build();
    }

    /**
     * 声明死信队列
     */
    @Bean("DeadLetterQueue")
    public Queue DeadLetterQueue() {
        return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
    }

    /**
     * 声明备份队列
     */
    @Bean("BackupQueue")
    public Queue BackupQueue() {
        return QueueBuilder.durable(BACKUP_QUEUE).build();
    }

    /**
     * 声明报警队列
     */
    @Bean("WarningQueue")
    public Queue WarningQueue() {
        return QueueBuilder.durable(WARNING_QUEUE).build();
    }

    /**
     * 绑定较快队列与正常交换机
     */
    @Bean
    public Binding CommonFasterQueueBindingCommonSwitch(
            @Qualifier("CommonFasterQueue") Queue commonFasterQueue,
            @Qualifier("CommonSwitch") DirectExchange commonSwitch) {
        return BindingBuilder.bind(commonFasterQueue).to(commonSwitch).with(COMMON_SWITCH_TO_COMMON_FASTER_QUEUE_PATH);
    }
    /**
     * 绑定较慢队列与正常交换机
     */
    @Bean
    public Binding CommonSlowerQueueBindingCommonSwitch(
            @Qualifier("CommonSlowerQueue") Queue CommonSlowerQueue,
            @Qualifier("CommonSwitch") DirectExchange commonSwitch) {
        return BindingBuilder.bind(CommonSlowerQueue).to(commonSwitch).with(COMMON_SWITCH_TO_COMMON_SLOWER_QUEUE_PATH);
    }
    /**
     * 绑定任意时间队列与正常交换机
     */
    @Bean
    public Binding CommonAllTimeQueueBindingCommonSwitch(
            @Qualifier("CommonAllTimeQueue") Queue commonAllTimeQueue,
            @Qualifier("CommonSwitch") DirectExchange commonSwitch) {
        return BindingBuilder.bind(commonAllTimeQueue).to(commonSwitch).with(COMMON_SWITCH_TO_COMMON_ALL_TIME_QUEUE_PATH);
    }
    /**
     * 绑定死信队列与死信交换机
     */
    @Bean
    public Binding DeadLetterQueueBindingDeadLetterSwitch(
            @Qualifier("DeadLetterQueue") Queue DeadLetterQueue,
            @Qualifier("DeadLetterSwitch") DirectExchange DeadLetterSwitch) {
        return BindingBuilder.bind(DeadLetterQueue).to(DeadLetterSwitch).with(COMMON_ALL_QUEUE_TO_DEAD_LETTER_SWITCH_PATH);
    }
    /**
     * 绑定备份队列与备份交换机
     */
    @Bean
    public Binding BackupQueueBindingBackupSwitch(
            @Qualifier("BackupQueue") Queue BackupQueue,
            @Qualifier("BackupSwitch") FanoutExchange BackupSwitch) {
        return BindingBuilder.bind(BackupQueue).to(BackupSwitch);
    }
    /**
     * 绑定报警队列与备份交换机
     */
    @Bean
    public Binding WarningQueueBindingBackupSwitch(
            @Qualifier("WarningQueue") Queue WarningQueue,
            @Qualifier("BackupSwitch") FanoutExchange BackupSwitch) {
        return BindingBuilder.bind(WarningQueue).to(BackupSwitch);
    }

}
